package alipay.openplatform.edu.campus.jiuye.service.services.topic;

import alipay.openplatform.edu.campus.jiuye.common.enums.TopicDetailInfoStateEnum;
import alipay.openplatform.edu.campus.jiuye.common.enums.TopicInfoEnum;
import alipay.openplatform.edu.campus.jiuye.common.exception.OnlineRuntimeException;
import alipay.openplatform.edu.campus.jiuye.common.utils.ObjectUtils;
import alipay.openplatform.edu.campus.jiuye.dao.mappers.system.AreaInfoMapper;
import alipay.openplatform.edu.campus.jiuye.dao.mappers.topic.TopicCityInfoMapper;
import alipay.openplatform.edu.campus.jiuye.dao.mappers.topic.TopicDetailInfoMapper;
import alipay.openplatform.edu.campus.jiuye.dao.mappers.topic.TopicInfoMapper;
import alipay.openplatform.edu.campus.jiuye.dao.mappers.topic.TopicSchoolInfoMapper;
import alipay.openplatform.edu.campus.jiuye.dao.mappers.user.UserTopicInfoMapper;
import alipay.openplatform.edu.campus.jiuye.model.po.*;
import alipay.openplatform.edu.campus.jiuye.model.vo.CityVO;
import alipay.openplatform.edu.campus.jiuye.model.vo.ImgUrlVO;
import alipay.openplatform.edu.campus.jiuye.model.vo.SchoolVO;
import alipay.openplatform.edu.campus.jiuye.model.vo.TopicInfoVO;
import alipay.openplatform.edu.campus.jiuye.service.iservice.topic.ITopicCityInfoService;
import alipay.openplatform.edu.campus.jiuye.service.iservice.topic.ITopicDetailInfoService;
import alipay.openplatform.edu.campus.jiuye.service.iservice.topic.ITopicImgInfoService;
import alipay.openplatform.edu.campus.jiuye.service.iservice.topic.ITopicSchoolInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class TopicDetailInfoService implements ITopicDetailInfoService {
    private final static Logger log = LoggerFactory.getLogger(TopicDetailInfoService.class);

    @Autowired
    private TopicDetailInfoMapper topicDetailInfoMapper;
    @Autowired
    private TopicInfoMapper topicInfoMapper;
    @Autowired
    private UserTopicInfoMapper userTopicInfoMapper;
    @Autowired
    private ITopicCityInfoService iTopicCityInfoService;
    @Autowired
    private ITopicImgInfoService iTopicImgInfoService;
    @Autowired
    private ITopicSchoolInfoService iTopicSchoolInfoService;
    @Autowired
    private TopicSchoolInfoMapper topicSchoolInfoMapper;
    @Autowired
    private AreaInfoMapper areaInfoMapper;
    @Autowired
    private TopicCityInfoMapper topicCityInfoMapper;


    @Override
    public int deleteByPrimaryKey(Integer topicId) {
        return 0;
    }

    @Override
    public int insert(int topicId, String content) {
        TopicDetailInfoPO topicDetailInfoPO = new TopicDetailInfoPO(topicId, content);
        return topicDetailInfoMapper.insert(topicDetailInfoPO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(TopicDetailInfoPO record) {
        return 0;
    }

    @Override
    public TopicDetailInfoPO selectByPrimaryKey(Integer topicId) {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByTopicId(int topicId, String content) {
        TopicDetailInfoPO topicDetailInfoPO = new TopicDetailInfoPO();
        topicDetailInfoPO.setTopicId(topicId);
        topicDetailInfoPO.setContent(content);
        return topicDetailInfoMapper.updateByTopicId(topicDetailInfoPO);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(TopicDetailInfoPO record) {
        return 0;
    }

    /**
     * 前端接口
     *
     * @param topicId
     * @param userId
     * @return
     */
    @Override
    public TopicInfoVO selectDetailInfoByTopicID(String userId, String cityId, String schoolId, Integer topicId, String schoolName, String cityName) {
        log.info("查看前台主题详细信息[TopicDetailInfoService][selectDetailInfoByTopicID]param{userId :" + userId + "," + "topicId:" + topicId + "}");

        if (ObjectUtils.hasEmpty(userId, topicId)) {
            log.error("[TopicDetailInfoService][selectDetailInfoByTopicID]check userid,topicId is Null }");
            throw new OnlineRuntimeException("PARAM_ERROR", "参数不合法");
        }

        TopicDetailInfoPO topicDetailInfoPO = topicDetailInfoMapper.selectDeailInfo(topicId);
        TopicInfoPO topicInfoPO = topicInfoMapper.selectTopicInfo(topicId);

        UserTopicInfoPO userTopicInfoPO = null;
        if (ObjectUtils.hasNull(topicDetailInfoPO, topicInfoPO)) {
            throw new OnlineRuntimeException("PARAM_NULL", "参数NULL");
        }

        userTopicInfoPO = userTopicInfoMapper.selectSchoolTopicInfo(userId, topicId, schoolId, cityId);


        List<ImgUrlVO> listImage = iTopicImgInfoService.getImgUrlsByTopicId(topicId);

        TopicInfoVO topicInfoVO = new TopicInfoVO();
        topicInfoVO.setTopicId(topicInfoPO.getTopicId());
        if (topicInfoPO.getTopicType() == 1) {
            topicInfoVO.setTopicTitle(topicInfoPO.getTopicTitle() + "【" + schoolName + "】");
        } else {
            topicInfoVO.setTopicTitle(topicInfoPO.getTopicTitle() + "【" + cityName + "】");
        }
        topicInfoVO.setCreatetime(topicInfoPO.getCreatetime());// add by yuxi  添加创建时间
        topicInfoVO.setContent(topicDetailInfoPO.getContent());
        topicInfoVO.setStarttime(topicInfoPO.getStarttime());
        if (topicInfoPO.getState() != null) {
            topicInfoVO.setState(topicInfoPO.getState().intValue());
        }
        if (topicInfoVO.getStarttime().before(new Date()) || (topicInfoVO.getStarttime().after(new Date()) && topicInfoVO.getState() == 1)) {
            topicInfoVO.setIsExpire(1);//过期
        } else {
            topicInfoVO.setIsExpire(0);//过期
        }

        topicInfoVO.setTopicType(topicInfoPO.getTopicType());
        topicInfoVO.setIfLine(topicInfoPO.getIfLine());
        topicInfoVO.setPlace(topicInfoPO.getPlace());
        //topicInfoVO.setSchools(listSchool);
        //设置一个Cityid  cityName


        //topicInfoVO.setCities(listCity);
        topicInfoVO.setImgUrls(listImage);
        topicInfoVO.setTopicName(TopicInfoEnum.getMessage(topicInfoPO.getTopicType()));
        if (userTopicInfoPO == null) {// add by yuxi update status
            topicInfoVO.setId(null);
            topicInfoVO.setCityName(cityName);
            topicInfoVO.setCityId(cityId);
            topicInfoVO.setSchoolId(schoolId);
            topicInfoVO.setSchoolName(schoolName);
            topicInfoVO.setState(TopicDetailInfoStateEnum.COLLECT.getCode()); //int
        } else {
            topicInfoVO.setId(userTopicInfoPO.getId());
            topicInfoVO.setCityName(userTopicInfoPO.getCityName());
            topicInfoVO.setCityId(cityId);
            topicInfoVO.setSchoolId(schoolId);
            topicInfoVO.setSchoolName(userTopicInfoPO.getSchoolName());
            topicInfoVO.setState(userTopicInfoPO.getState().intValue());
        }
        // log.info("查看前台主题详细信息[TopicDetailInfoService][selectDetailInfoByTopicID()]" + JSON.toJSONString(topicInfoVO));
        return topicInfoVO;

    }

    /**
     * 后台查询详情
     *
     * @param topicId
     * @return
     */
    @Override
    public TopicInfoVO selectDetailInfoByTopicID(Integer topicId) {
        log.info("查看后台主题详细信息[TopicDetailInfoService][selectDetailInfoByTopicID]param{topicId :" + topicId + "}");
        if (ObjectUtils.hasEmpty(topicId)) {
            log.error("[TopicDetailInfoService][selectDetailInfoByTopicID]check userid,topicId is Null }");
            throw new OnlineRuntimeException("PARAM_ERROR", "参数不合法");
        }
        TopicDetailInfoPO topicDetailInfoPO = topicDetailInfoMapper.selectDeailInfo(topicId);
        TopicInfoPO topicInfoPO = topicInfoMapper.selectTopicInfo(topicId);
        List<CityVO> listCity = iTopicCityInfoService.getCitiesByTopicId(topicId);
        List<ImgUrlVO> listImage = iTopicImgInfoService.getImgUrlsByTopicId(topicId);
        List<SchoolVO> listSchool = iTopicSchoolInfoService.getSchoolsByTopicId(topicId);
        TopicInfoVO topicInfoVO = new TopicInfoVO();
        if (ObjectUtils.hasEmpty(topicDetailInfoPO, topicInfoPO)) {
            log.error("[TopicDetailInfoService][selectDetailInfoByTopicID]check topicDetailInfoPO is " + topicDetailInfoPO + " topicInfoPO" + topicInfoPO + "}");
            throw new OnlineRuntimeException("PARAM_NULL", "参数NULL");
        }
        topicInfoVO.setTopicTitle(topicInfoPO.getTopicTitle());
        //增加createtime
        topicInfoVO.setCreatetime(topicInfoPO.getCreatetime());
        topicInfoVO.setContent(topicDetailInfoPO.getContent());
        topicInfoVO.setStarttime(topicInfoPO.getStarttime());
        topicInfoVO.setTopicType(topicInfoPO.getTopicType());
        topicInfoVO.setIfLine(topicInfoPO.getIfLine());
        topicInfoVO.setPlace(topicInfoPO.getPlace());
        topicInfoVO.setSchools(listSchool);
        topicInfoVO.setCities(listCity);
        topicInfoVO.setImgUrls(listImage);
        topicInfoVO.setTopicName(TopicInfoEnum.getMessage(topicInfoPO.getTopicType()));
        // log.info("查看后台主题详细信息[TopicDetailInfoService][selectDetailInfoByTopicID()]" + JSON.toJSONString(topicInfoVO));
        return topicInfoVO;

    }

    @Override
    public void deleteDetailByTopicId(Integer topicId) {
        topicDetailInfoMapper.deleteByPrimaryKey(topicId);
    }

    /**
     * @param userId
     * @param topicId
     * @param schoolId
     * @param cityId
     * @return
     */
    @Override
    public TopicInfoVO selectDetailInfoByTopicIdAndSchoolId(String userId, Integer topicId, String schoolId, String cityId) {
        log.info("查看详细信息[TopicDetailInfoService][selectDetailInfoByTopicIdAndSchoolId]param{userId :" + userId + "," + "topicId:" + topicId + ",schoolId :" + schoolId + ",cityId: " + cityId + "}");


        //从topic_school表中查询信息
        TopicSchoolInfoPO topicSchoolInfoPO;
        String cityName = "未知";
        String schoolName = "未知";
        //1cityid为零
        if ("0".equals(cityId) || "".equals(cityId.trim()) || cityId.trim().equals("null")) {
            cityId = "0";
            topicSchoolInfoPO = topicSchoolInfoMapper.selectSchoolInfoByTopicIdAndSchoolId(topicId, schoolId, null);
            schoolName = topicSchoolInfoPO.getSchoolName();
            //schoolid为零
        } else if ("0".equals(schoolId) || "".equals(schoolId.trim()) || schoolId.trim().equals("null")) {
            schoolId = "0";
            TopicCityInfoPO topicCityInfoPO = topicCityInfoMapper.selectByTopicIdAndCityid(topicId.toString(), cityId);
            cityName = topicCityInfoPO.getAreaName();
        } else {
            //点击轮播图 查询
            topicSchoolInfoPO = topicSchoolInfoMapper.selectSchoolInfoByTopicIdAndSchoolId(topicId, schoolId, cityId);
            schoolName = topicSchoolInfoPO.getSchoolName();
            cityName = areaInfoMapper.getCityNameById(Integer.valueOf(topicSchoolInfoPO.getCityId()));
        }
        TopicInfoVO topicInfoVO = selectDetailInfoByTopicID(userId, cityId, schoolId, topicId, schoolName, cityName);
        return topicInfoVO;
    }
}
